package com.kylin.module.exhibition.actions;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.Order;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.exhibition.business.ExhibitionBlogic;
import com.kylin.module.exhibition.forms.ExhibitionForm;
import com.kylin.module.exhibition.orm.Exhibition;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;
/**
 * 会展信息部分的实现
 * 
 * @author DH
 * @version 03/10/08
 * @see com.kylin.struts.BaseDispatchAction
 * @see com.kylin.struts.BaseActionInterface
 */
public class ExhibitionAction extends BaseDispatchAction implements
		BaseActionInterface {
	protected static Logger log =Logger.getLogger(ExhibitionAction.class);
	/**
	 * 前台会展信息列表
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward exhibitionList(ActionMapping mapping,ActionForm actionForm,HttpServletRequest request,HttpServletResponse response)
	{
		log.info("查询前台会展信息列表");
		String forward = "exhibitionList";
		String searchId = request.getParameter("searchId");
		int pageSize = 20;
		int pageNum = 1;
		CriteriaInfo cinfo = null;
		try
		{
			if(request.getParameter("page") != null)
			{
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if(searchId == null)	
			{
				List conditions = new LinkedList();
				conditions.add(new PropertyCondition("language",getSessionLocale(request).toString(),PropertyCondition.OPERATOR_EQ));				
				// 产品下拉菜单条件，分为一级分类和二级分类
				if( hasParam(request.getParameter("parentCat")) ){
					Dict parent = (Dict)BaseHibernateDAO.findById(Dict.class,new Long(request.getParameter("parentCat")));
					conditions.add(new PropertyCondition("catExhibition.parent",parent,PropertyCondition.OPERATOR_EQ));
					request.setAttribute("searchCatProduct",parent);
				}else if( hasParam(request.getParameter("catproduct"))){
					Long catid = new Long(request.getParameter("catproduct"));
					conditions.add(new PropertyCondition("catExhibition.id",catid,PropertyCondition.OPERATOR_EQ));
					request.setAttribute("searchCatProduct",BaseHibernateDAO.findById(Dict.class, catid));
				}
				if(hasParam(request.getParameter("country")))
				{
					String country = request.getParameter("country");
					conditions.add(new PropertyCondition("country",country,PropertyCondition.OPERATOR_EQ));
				}
				if( hasParam(request.getParameter("catid")))
				{
					String catid = request.getParameter("catid");
				    conditions.add(new PropertyCondition("catExhibition.id",new Long(catid),PropertyCondition.OPERATOR_EQ ));
				}
				PropertyCondition[] array = (PropertyCondition[])conditions.toArray(new PropertyCondition[0]);
				cinfo = new CriteriaInfo(Exhibition.class,array,Order.desc("id"));
				searchId = this.addUserCacheCriteriaInfo(request, cinfo);
				request.setAttribute(Constants.KEY_REQUEST_QUERYID, searchId);
			}
			cinfo = getUserCacheCriteriaInfo(request, searchId);
			if(cinfo == null)
				throw new BusinessException("长时间未刷新页面，上次查询已失效，请重新访问上个页面");
			DisplayPage page = PageManager.getDisplayPage(pageSize, pageNum, cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);
		}catch(Exception e)
		{
			doException(request,e,forward);
		}
		return mapping.findForward(forward);
	}
	/**
	 * 后台添加新的会展信息
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward add(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.info("添加会展信息");
		String forward = "addSucc";
		ExhibitionForm form = (ExhibitionForm)actionForm;
		try
		{
			ExhibitionBlogic logic = new ExhibitionBlogic();
			logic.add(form);
			request.setAttribute(Constants.MSGKEY,new MsgKey("","添加信息成功"));
		}catch(Exception e)
		{
			doException(request,e,forward);
		}
		return mapping.findForward(forward);
	}
	/**
	 * 根据ID删除一条会展信息
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		log.debug("删除会展信息"+id);
		try
		{
			ExhibitionBlogic logic = new ExhibitionBlogic();
			logic.deleteById(new Long(id));
			request.setAttribute(Constants.MSGKEY,new MsgKey("","删除成功"));
		}catch(Exception e)
		{
			log.error(e);
			e.printStackTrace();
		}
		return this.search(mapping, actionForm, request, response);
	}
	/**
	 * 查询会展信息
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward search(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("会展信息查询");
		String forward = "list";
		String searchId = request.getParameter("searchId");
		int pageSize = 15;
		int pageNum = 1;
		CriteriaInfo cinfo = null;
		try
		{
			if(request.getParameter("page") != null)
			{
				pageNum = Integer.parseInt(request.getParameter("page"));
			}
			if(searchId == null)
			{
				List conditions = new LinkedList();
				/*查询条件:语言*/
				String language = request.getParameter("language");
				if(language != null && !language.equals(""))
				{
					conditions.add(new PropertyCondition("language",language,PropertyCondition.OPERATOR_EQ));
				}
				/*查询条件：国家*/
				String country = request.getParameter("keyword_country");
				if(country != null && !country.equals(""))
				{
					conditions.add(new PropertyCondition("country",country,PropertyCondition.OPERATOR_EQ));
				}
				/*查询条件：标题*/
				String title = request.getParameter("keyword_title");
				if(title != null && !title.equals(""))
				{
					conditions.add(new PropertyCondition("title","%"+title+"%",PropertyCondition.OPERATOR_LIKE));
				}
				/*查询条件:分类*/
				if( hasParam(request.getParameter("catproduct")))
				{
					String catid = request.getParameter("catproduct");
				    conditions.add(new PropertyCondition("catExhibition.id",new Long(catid),PropertyCondition.OPERATOR_EQ ));
				}
				cinfo = new CriteriaInfo(Exhibition.class,conditions);
				cinfo.setOrderby(Order.desc("id"));
				addUserCacheCriteriaInfo(request,cinfo);
			}
			else
			{
				cinfo = getUserCacheCriteriaInfo(request,searchId);
			}
			DisplayPage dispage = PageManager.getDisplayPage(pageSize,pageNum,cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT,dispage);
		}catch(Exception e)
		{
			doException(request,e,forward);
		}
		return mapping.findForward(forward);
	}
	/**
	 * 根据ID获得需要更新的会展信息
	 * 
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		log.debug("获得更新会展信息ID"+id);
		String forward = "update";
		try
		{
			Exhibition obj = null;
			if(id != null)
			{
				ExhibitionBlogic logic = new ExhibitionBlogic();
				obj = logic.getById(new Long(id));
			}
			request.setAttribute(Constants.KEY_REQUEST_RESULT, obj);
		}catch(Exception e)
		{
			doException(request,e,forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward executeUpdate(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		log.debug("开始更新会展信息:"+id);
		ExhibitionForm form = (ExhibitionForm)actionForm;
		try
		{
			if(id != null)
			{
				ExhibitionBlogic logic = new ExhibitionBlogic();
				logic.update(form);
				request.setAttribute(Constants.MSGKEY, new MsgKey(
						"msg.addSpecial.succ", "修改成功"));
			}
		}catch(Exception e)
		{
			log.error(e);
			e.printStackTrace();
		}
		return this.search(mapping, actionForm, request, response);
	}
	
	public ActionForward detail(ActionMapping mapping,ActionForm actionForm,HttpServletRequest request,HttpServletResponse response){
		String id = request.getParameter("id");
		log.debug("查询会展信息详细信息ID为:"+id);
		String forward = "detail";
		try{
			ExhibitionBlogic logic = new ExhibitionBlogic();
			Exhibition obj = logic.getById(new Long(id));
			request.setAttribute(Constants.KEY_REQUEST_RESULT,obj);
		}catch(Exception e){
			doException(request,e,forward);
		}
		return mapping.findForward(forward);
	}
}
